package de.slothsoft.wasbinich.core.database.webservice;

import java.util.Locale;

import de.slothsoft.wasbinich.core.ApplicationInfo;
import de.slothsoft.wasbinich.core.WebserviceUrls;
import de.slothsoft.wasbinich.core.database.ApplicationBean;
import de.slothsoft.wasbinich.core.database.DatabaseException;

public abstract class AbstractWebserviceApplicationBean extends AbstractWebserviceBean implements ApplicationBean {

    public AbstractWebserviceApplicationBean(RestCommunicator communicator) {
	super(communicator);
    }

    @Override
    public ApplicationInfo getServerInfo() throws DatabaseException {
	String json = get(WebserviceUrls.getApplicationInfoUrl());
	return getApplicationInfoFromString(json);
    }

    protected static ApplicationInfo getApplicationInfoFromString(String json) throws DatabaseException {
	return GSON.fromJson(json, ApplicationInfo.class);
    }

    protected static Locale[] parseLocales(String languages) {
	return parseLocales(languages.split(StringArrayTypeAdapter.SEPARATOR));
    }

    protected static Locale[] parseLocales(String[] languages) {
	Locale[] result = new Locale[languages.length];
	for (int i = 0; i < result.length; i++) {
	    result[i] = new Locale(languages[i]);
	}
	return result;
    }
}
